import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Transfer,Form } from 'antd';
import action from "../../../store/action";



class index extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            mockData: [],
            targetKeys: [],
        };
    }
    componentDidMount() {
        this.getMock();
    }
    render() {
        return (<Form.Item name="transfer" initialValue label="权限:" initialValue={this.state.targetKeys}>
            <Transfer
                style={{ width: 'auto', height: 300 }}
                dataSource={this.state.mockData}
                targetKeys={this.state.targetKeys}
                onChange={this.handleChange}
                titles={['单位列表', '有权查看的单位列表']}
                onSearch={this.handleSearch}
                render={item => item.title}
                showSearch

            />
        </Form.Item>)
    }
    getMock = () => {
        if (this.props.manage_EditData.length == 0) {
            let targetKeys = [];
            let mockData = [];
            return this.$axios.get(`/manager/api/getUserPermissionsOrganization/0`).then((res) => {
                let newList = {};
                [].push.apply(newList, res.notSelectorganizationList);
                const { managementAdd } = this.props;
                managementAdd(newList)
                res.notSelectorganizationList.map((item, index) => {
                    let data = {
                        key: index.toString(),
                        title: item.pname,
                        description: `description of content${item + 1}`,
                        chosen: Math.random() * 2 > 1,
                    };
                    if (data.chosen) {
                        targetKeys.push(data.key);
                    }
                    mockData.push(data);
                })
                this.setState({ mockData });
            }).catch((err) => {
                console.log(err);
            })
        }
        else {
            let targetKeys = [];
            let mockData = [];
            let new_val = this.props.manage_SaveVal.concat(this.props.manage_Second_SaveVal);
            this.props.manage_Second_SaveVal.length != 0 ? (
                this.props.manage_Second_SaveVal.map((j, val_index) => {
                    new_val.map((item, index) => {
                        let data = {
                            key: index.toString(),
                            title: item.pname,
                            chosen: item.id,
                        };
                        if (j.id == data.chosen) {
                            targetKeys.push(data.key);
                        }
                        if (val_index == 0) {
                            mockData.push(data);
                        }
                    });
                })
            ) : console.log()
            this.setState({ mockData, targetKeys });
        };
    };



    handleChange = targetKeys => {
        this.setState({ targetKeys });

    };

    handleSearch = (dir, value) => {
        console.log('search:', dir, value);
    };
}
export default connect(state => ({ ...state.custom }), action.custom)(index)